<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/1/15
 * Time: 17:33
 */

namespace App\Models\CacheModel\zl7;

use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zl7QuestionAiAnswerRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl7_question_ai_answer_record';
    protected $cacheModel = 'ai_answer.user_record';

    /**
     * 获取用户提问AI信息
     * @param $type
     * @param $uid
     * @param $id
     * @return array
     * @throws \Exception
     */
    public function getUserAiInfo($type, $uid, $id)
    {
        $aiIds = $this->getUserAiIds($uid, $type);
        $res = [
            'id' => 0,
            'content' => null,
            'num_type' => 0
        ];
        foreach ($aiIds as $aiId => $did)
        {
            if ($id == $did)
            {
                $res['id'] = $aiId;
                $record = $this->getInfoById($aiId);
                $res['content'] = $record['content'];
                $res['num_type'] = $record['num_type'];
                break;
            }
        }
        return $res;
    }

    /**
     * @param $uid
     * @param $type 1=>骰子  2=>塔罗
     * @return array|mixed
     * @throws \Exception
     */
    public function getUserAiIds($uid, $type)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($type)->setSort('desc')->ZsGetIocData('list', function () use ($type, $uid)
        {
            //活动只有折扣类型
            $where = ['type' => $type, 'uid' => $uid];
            try
            {
                $ids = $this->where($where)->order('id', 'DESC')->column('did', 'id');
            }
            catch (\Exception $e)
            {
                $ids = [];
            }
            return $ids;
        }, true);
    }

    /**
     * 添加AI解答数据
     * @param $uid
     * @param $type
     * @param $data
     * @return void
     */
    public function saveData($uid, $type, $data)
    {
        try
        {
            $this->save($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($type)->rmCache('list');
        }
        catch (\Exception $e)
        {
            Log::write('Zl7QuestionAiAnswerRecord model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }

    /**
     * 更新Ai解答数据
     * @param $id
     * @param $data
     * @return void
     */
    public function updateData($id, $data)
    {
        try
        {
            $this->where('id', $id)->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        }
        catch (\Exception $e)
        {
            Log::write('Zl7QuestionAiAnswerRecord model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            return $this->where(['id' => ['in', $ids]])->order('id desc')->column('content,num_type', 'id');
        }
        catch (\Exception $e)
        {
            Log::write('Zl7QuestionAiAnswerRecord model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return [];
        }
    }
}
